//
// Created by lymk on 2022/3/21.
//

#include "BoxMesh.h"

void BoxMesh::GetVertex(std::vector<VertexIn> &InVertex,std::vector<std::vector<int>>& idx) {
    VertexIn tmp;
    for(int i=0;i<index.size();i++){
        int start = InVertex.size();
        for(int j=0;j<3;j++) {
            tmp.pos = {point[index[i][j]], 1.f};
            tmp.color = color[index[i][j]];
            tmp.texCoord = {0.f, 0.f};
            tmp.normal = normal[i];
            InVertex.push_back(tmp);
        }
        idx.emplace_back(std::vector<int>({start,start+1,start+2}));
    }
}

BoxMesh::BoxMesh(float w, float h, float d) {
    width = w;
    height = h;
    depth = d;
    Init();
}

void BoxMesh::Init() {
    point.resize(8);
    point[0] = {width/2.f,-height/2.f,-depth/2.f};
    point[1] = {width/2.f,height/2.f,-depth/2.f};
    point[2] = {-width/2.f,height/2.f,-depth/2.f};
    point[3] = {-width/2.f,-height/2.f,-depth/2.f};
    point[4] = {width/2.f,-height/2.f,depth/2.f};
    point[5] = {width/2.f,height/2.f,depth/2.f};
    point[6] = {-width/2.f,height/2.f,depth/2.f};
    point[7] = {-width/2.f,-height/2.f,depth/2.f};

    //index.resize(12,std::vector<int>(3));


    index.emplace_back(std::vector<int>({5,6,7}));
    index.emplace_back(std::vector<int>({2,6,7}));
    index.emplace_back(std::vector<int>({2,7,3}));
    index.emplace_back(std::vector<int>({0,1,3}));
    index.emplace_back(std::vector<int>({0,1,4}));
    index.emplace_back(std::vector<int>({1,5,4}));
    index.emplace_back(std::vector<int>({1,2,3}));
    index.emplace_back(std::vector<int>({0,3,7}));
    index.emplace_back(std::vector<int>({0,7,4}));
    index.emplace_back(std::vector<int>({4,5,7}));
    index.emplace_back(std::vector<int>({2,6,5}));
    index.emplace_back(std::vector<int>({1,2,5}));

    normal.emplace_back(glm::vec3(0.f,0.f,1.f));
    normal.emplace_back(glm::vec3(-1.f,0.f,0.f));
    normal.emplace_back(glm::vec3(-1.f,0.f,0.f));
    normal.emplace_back(glm::vec3(0.f,0.f,-1.f));
    normal.emplace_back(glm::vec3(1.f,0.f,0.f));
    normal.emplace_back(glm::vec3(1.f,0.f,0.f));
    normal.emplace_back(glm::vec3(0.f,0.f,-1.f));
    normal.emplace_back(glm::vec3(0.f,-1.f,0.f));
    normal.emplace_back(glm::vec3(0.f,-1.f,0.f));
    normal.emplace_back(glm::vec3(0.f,0.f,1.f));
    normal.emplace_back(glm::vec3(0.f,1.f,0.f));
    normal.emplace_back(glm::vec3(0.f,1.f,0.f));

    color.emplace_back(glm::vec4(1.f,0.f,0.f,1.f));
    color.emplace_back(glm::vec4(1.f,0.f,0.f,1.f));
    color.emplace_back(glm::vec4(1.f,0.f,0.f,1.f));
    color.emplace_back(glm::vec4(1.f,0.f,0.f,1.f));
    color.emplace_back(glm::vec4(1.f,0.f,0.f,1.f));
    color.emplace_back(glm::vec4(1.f,0.f,0.f,1.f));
    color.emplace_back(glm::vec4(1.f,0.f,0.f,1.f));
    color.emplace_back(glm::vec4(1.f,0.f,0.f,1.f));

    /*
    color.emplace_back(glm::vec4(0.f,1.f,0.f,1.f));
    color.emplace_back(glm::vec4(0.f,0.f,1.f,1.f));
    color.emplace_back(glm::vec4(1.f,0.f,0.f,1.f));
    color.emplace_back(glm::vec4(0.f,1.f,0.f,1.f));
    color.emplace_back(glm::vec4(0.f,0.f,1.f,1.f));
    color.emplace_back(glm::vec4(1.f,0.f,0.f,1.f));
    color.emplace_back(glm::vec4(0.f,1.f,0.f,1.f));*/
}

void BoxMesh::SetColor(glm::vec4 c) {
    for(int i=0;i<color.size();i++)
        color[i] = c;
}
